<?php
/**
 * ============================================================================
 * Copyright (c) 2015-2018 贵州大师兄信息技术有限公司 All rights reserved.
 * siteַ: http://www.dsxcms.com
 * ============================================================================
 * @author:     David Song<songdewei@163.com>
 * @version:    v1.0.0
 * ---------------------------------------------
 * Date: 2018/8/29
 * Time: 上午11:55
 */

namespace App\WeChat\WxPay\Order;


class WxPayBizPayUrl extends WxPayOrder
{
    protected $values = [
        'time_stamp'=>null,
        'nonce_str'=>null,
        'product_id'=>null
    ];

    /**
     * 设置支付时间戳
     * @param string $value
     **/
    public function setTimetamp($value = null){
        if (is_null($value)) {
            $this->values['time_stamp'] = time();
        }else {
            $this->values['time_stamp'] = $value;
        }
    }

    /**
     * 获取支付时间戳的值
     * @return string 值
     **/
    public function getTimestamp(){
        return $this->values['time_stamp'];
    }

    /**
     * 设置随机字符串
     * @param string $value
     **/
    public function setNonceStr($value = null)
    {
        if (is_null($value)) {
            $this->values['nonce_str'] = md5(time().rand(100,999));
        }else {
            $this->values['nonce_str'] = $value;
        }
    }

    /**
     * 获取随机字符串的值
     * @return string 值
     **/
    public function getNonceStr(){
        return $this->values['nonce_str'];
    }

    /**
     * 设置商品ID
     * @param string $value
     **/
    public function setProductId($value){
        $this->values['product_id'] = $value;
    }

    /**
     * 获取商品ID的值
     * @return  string 值
     **/
    public function getProductId(){
        return $this->values['product_id'];
    }

    /**
     * @return string
     */
    public function getBizUrl()
    {
        return "weixin://wxpay/bizpayurl?" . $this->buildParams($this->values);
    }

    /**
     *
     * 参数数组转换为url参数
     * @param array $urlObj
     * @return string
     */
    private function buildParams($urlObj){
        $buff = "";
        foreach ($urlObj as $k => $v)
        {
            if ($v && !is_array($v)) {
                $buff .= $k . "=" . $v . "&";
            }
        }

        $buff = trim($buff, "&");
        return $buff;
    }
}
